package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.net.SharedObject;
	import flash.net.URLRequest;
	import flash.net.registerClassAlias;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import ui.Button;
	import ui.EventPlayCmd;
	import ui.List;
	import ui.ListItem;
	import ui.Panel;
	import ui.ScrollBarV;
	
	public class BgPanel extends Sprite
	{
		private		const LEFT:int = 13;
		private		const BOTTOM:int = 30;
		private		const MARGIN:int = 2;
		
		private		var		isPlay:Boolean = false;
		private		var		pauseBtn:Button = null;
		private		var		playBtn:Button = null;
		private		var		progressBar:ProgressBar = null;
		private		var		titleBar:TitleBar = null;
		private		var		playTimeField:TextField = null;
		
		private		var		list:List = null;
		private		var		scrollBar:ScrollBarV = null;
		
		private		var		sound:MusicSound = null;
		
		private		var		addMusicBtn:Button = null;
		private		var		rmMusicBtn:Button = null;
		
		private		var		addMusicPanel:AddMusicPanel = null;
		private		var		rmMusicPannel:Panel = null;
		
		private		var		curPlayMusic:String = "";//当前播放音乐
		
		private		var		testNum:int = 0;
		public function BgPanel()
		{
			super();
			
			this.graphics.beginFill(0x77C4D7E9, 1);
			this.graphics.drawRoundRect(0,0,300, 400, 10, 10);
			this.graphics.endFill();
			
			onInit();
		}
		
		public	function	onInit():void{
			
			//标题
			this.titleBar = new TitleBar("");
			this.titleBar.x = (this.width - this.titleBar.width)/2;
			this.titleBar.y = 15;
			this.addChild(this.titleBar);
			
			//进度条
			this.progressBar = new ProgressBar();
			this.progressBar.x = this.LEFT;
			this.progressBar.y = 45;
			this.addChild(this.progressBar);
			
			//控制按钮
			var preBtn:Button = new Button(20, 20, Asset.getPrevImg());
			preBtn.x = progressBar.x;
			preBtn.y = 60;
			this.addChild(preBtn);
			
			this.pauseBtn = new Button(20, 20, Asset.getPauseImg());
			this.pauseBtn.x = preBtn.getRight()+this.MARGIN;
			this.pauseBtn.y = preBtn.getTop();
			this.pauseBtn.visible = false;
			this.addChild(this.pauseBtn);
			
			this.playBtn = new Button(20, 20, Asset.getPlayImg());
			this.playBtn.x = preBtn.getRight()+this.MARGIN;
			this.playBtn.y = preBtn.getTop();
			this.addChild(this.playBtn);

			var stopBtn:Button = new Button(20, 20, Asset.getStopImg());
			stopBtn.x = playBtn.getRight() + this.MARGIN;
			stopBtn.y = playBtn.getTop();
			this.addChild(stopBtn);
			
			var nextBtn:Button = new Button(20, 20, Asset.getNextImg());
			nextBtn.x = stopBtn.getRight() + this.MARGIN;
			nextBtn.y = stopBtn.getTop();
			this.addChild(nextBtn);
			
			//添加音乐按钮
			this.addMusicBtn = new Button(60, 20, Asset.getBtn2());
			this.addMusicBtn.x = preBtn.x;
			this.addMusicBtn.y = this.height - 30;
			this.addMusicBtn.text = "+ 添加";
			this.addMusicBtn.textColor = 0x927E60;
			this.addChild(this.addMusicBtn);
			
			//删除音乐按钮
			this.rmMusicBtn = new Button(60, 20, Asset.getBtn2());
			this.rmMusicBtn.x = this.addMusicBtn.getRight()+1;
			this.rmMusicBtn.y = this.addMusicBtn.y;
			this.rmMusicBtn.text = "+ 删除";
			this.rmMusicBtn.textColor = 0x927E60;
			this.addChild(this.rmMusicBtn);
			
			this.playTimeField = new TextField();
			this.playTimeField.text = "00:00 / 00:00";
			this.playTimeField.autoSize = TextFieldAutoSize.LEFT;
			this.playTimeField.x = this.progressBar.x + this.progressBar.width - this.playTimeField.width;
			this.playTimeField.y = stopBtn.getTop();
			this.playTimeField.textColor = 0xffffffff;
			this.addChild(this.playTimeField);
			
			this.initList();
			
			//添加音乐面板
			this.addMusicPanel = new AddMusicPanel(this);
			this.addMusicPanel.width = 249;
			this.addMusicPanel.height = 162;
			this.addMusicPanel.visible = false;
			this.addChild(this.addMusicPanel);
			
			//删除音乐面板
			this.rmMusicPannel = new DelMusicPanel(this);
			this.rmMusicPannel.width = 230;
			this.rmMusicPannel.height = 130;
			this.rmMusicPannel.visible = false;
			this.addChild(this.rmMusicPannel);
			
			playBtn.addEventListener(MouseEvent.CLICK, onPlayBtnClick);
			pauseBtn.addEventListener(MouseEvent.CLICK, onPauseBtnClick);
			preBtn.addEventListener(MouseEvent.CLICK, onPlayPreClick);
			nextBtn.addEventListener(MouseEvent.CLICK, onPlayNextClick);
			stopBtn.addEventListener(MouseEvent.CLICK, onStopBtnClick);
			this.addMusicBtn.addEventListener(MouseEvent.CLICK, onAddMusicBtnClick);
			this.rmMusicBtn.addEventListener(MouseEvent.CLICK, onRmMusicBtnClick);
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			this.list.addEventListener(EventPlayCmd.type, onPlayCmd);
		}
		
		public	function	initList():void{
			this.list = new List(this.width-40, 275);
			this.list.x = this.LEFT;
			this.list.y = 90;
			this.list.formatFun = formatItem;
			this.addChild(this.list);
			
			this.scrollBar = new ScrollBarV(15,this.list.height, 
											Asset.getScrollBarBtn(),
											Asset.getScrollBarBg(), 
											Asset.getScrollBarUpArrow(), 
											Asset.getScrollBarDownArrow());
			this.scrollBar.x = this.list.x+this.list.width-1;
			this.scrollBar.y = this.list.y;
			this.scrollBar.scrollView = this.list;
			this.addChild(this.scrollBar);
			
			//初始化播放列表
			var so:SharedObject = SharedObject.getLocal("musicPlayer");
			if (so.data.sl != undefined){
				var sl:StorageList = new StorageList();
				sl.fromString(so.data.sl);
				var itemList:Array = sl.getItemList();
				for(var i:int = 0; i < itemList.length; i++){
					var item:ListItem = itemList[i] as ListItem;
					this.list.addItem(item);
				}
			}
		}
		
		/**
		 * 格式化列表选项
		 */ 
		public	function	formatItem(field:TextField, item:ListItem, prefix:String):String{
			var str:String = prefix+item.name;
			while(field.textWidth < 250){
				str += " ";
				field.text = str+item.artist;
			}
			return field.text;
		}
		
		public	function	onSoundComplete(evt:Event):void{
			this.stopPlay();
			this.playNext();
		}
				
		public	function	onEnterFrame(evt:Event):void{
			if (this.sound != null){
				this.progressBar.percentDownload = this.sound.getLoadPercent();
				this.progressBar.percentPlay = this.sound.getPlayPercent();
				this.calculatePlayTime();
			}
		}
		
		public	function	calculatePlayTime():void{
			if (this.isPlay == false)
				return;
			
			var playPos:int = this.sound.getPlayPos();
			var curPlayMinute:int = playPos/(1000*60);
			var curPlaySecond:int = playPos/1000 - curPlayMinute*60;
			
			var soundLen:int = this.sound.getSoundLength();
			var soundLenMinute:int = soundLen/(1000*60);
			var soundLenSecond:int = soundLen/1000 - soundLenMinute*60;
		
			this.playTimeField.text = this.formatTimeNum(curPlayMinute)+":"
				+this.formatTimeNum(curPlaySecond)+" / "+
				this.formatTimeNum(soundLenMinute)+":"+
				this.formatTimeNum(soundLenSecond);
		}
		
		private	 function	formatTimeNum(value:int):String{
			var num:String = String(value);
			if (num.length == 1){
				return "0"+num;
			}
			else{
				return num;
			}
		}
		
		public	function	onStopBtnClick(evt:MouseEvent):void{
			this.stopPlay();
		}
		
		/**
		 * 停止播放歌曲
		 */ 
		public	function	stopPlay():void{
			if (this.sound == null)
				return;
			
			this.titleBar.stop = true;
			this.pauseBtn.visible = false;
			this.playBtn.visible = true;
			this.playTimeField.text = "00:00 / 00:00";
			this.isPlay = false;
			this.sound.stop();
		}
		
		/**
		 * 播放歌曲
		 */ 
		public	function	play(name:String, artist:String, url:String):Boolean{	
			if (this.sound != null){
				this.sound.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
			}
			
			var title:String = name+" - "+artist;
			this.titleBar.setTitle(title);
			this.sound = new MusicSound(url);
			var result:Boolean = this.sound.play();
			this.sound.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
			this.curPlayMusic = name;
			this.isPlay = true;
			this.pauseBtn.visible = true;
			this.playBtn.visible = false;
			this.titleBar.stop = false;
			return result;
		}
		
		public	function	onPlayPreClick(evt:MouseEvent):void{
			this.playPre();
		}
		
		public	function	onPlayNextClick(evt:MouseEvent):void{
			this.playNext();
		}
		
		public	function	onPlayBtnClick(evt:MouseEvent):void{
			onPlay();
		}
		
		public	function	onPauseBtnClick(evt:MouseEvent):void{
			this.isPlay = false;
			this.pauseBtn.visible = false;
			this.playBtn.visible = true;
			this.titleBar.stop = true;
			onPause();
		}
		
		public	function onPlay():void{
			var listItem:ListItem = this.list.getListItemFromIndex(0);
			if (listItem == null)
				return;
			
			if (this.sound != null){
				this.sound.play();
			}
			else{
				this.play(listItem.name, listItem.artist, listItem.url);
			}
			

		}
		
		public	function	onPause():void{
			this.sound.pause();
		}
		
		public	function	onAddMusicBtnClick(evt:MouseEvent):void{
			this.addMusicPanel.x = 18;
			this.addMusicPanel.y = this.height-this.addMusicPanel.height-50;
			this.addMusicPanel.visible = true;
		}
		
		public	function	onRmMusicBtnClick(evt:MouseEvent):void{
			this.rmMusicPannel.x = 30;
			this.rmMusicPannel.y = this.height-this.rmMusicPannel.height-50;
			this.rmMusicPannel.visible = true;
		}
		
		/**
		 * 返回播放列表
		 */ 
		public	function	getPlayList():List{
			return this.list;
		}
		
		/**
		 * 播放命令
		 */ 
		public	function	onPlayCmd(evt:EventPlayCmd):void{
			this.stopPlay();
			this.play(evt.name, evt.artist, evt.url);
			
			this.isPlay = true;
			this.pauseBtn.visible = true;
			this.playBtn.visible = false;
			this.titleBar.stop = false;
		}
		
		/**
		 * 播放下一首歌曲
		 */ 
		public	function	playNext():void{
			this.stopPlay();
			
			var index:int = this.list.getIndexFromName(this.curPlayMusic);
			index += 1;
			if (index >= this.list.itemNum){
				index = 0;
			}
			var item:ListItem = this.list.getListItemFromIndex(index);
			if (item == null)
				return;
			this.play(item.name, item.artist, item.url);
		}
		
		/**
		 * 播放上一首歌曲
		 */ 
		public	function	playPre():void{
			this.stopPlay();
			
			var index:int = this.list.getIndexFromName(this.curPlayMusic);
			index -= 1;
			if (index < 0)
				index = 0;
			var item:ListItem = this.list.getListItemFromIndex(index);
			if (item == null)
				return;
			this.play(item.name, item.artist, item.url);
		}
	}
}